package com.gingko.duominuo.data.source.local

import android.content.Context
import androidx.room.Room
import com.gingko.duominuo.data.source.local.dao.DownloadDao
import com.gingko.duominuo.data.source.local.dao.JobWorkDao
import com.gingko.duominuo.data.source.local.dao.NameMappingDao
import com.gingko.duominuo.data.source.local.dao.UserDao
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.android.components.ApplicationComponent
import dagger.hilt.android.qualifiers.ApplicationContext
import javax.inject.Singleton

@Module
@InstallIn(ApplicationComponent::class)
object RoomModule {

    /**
     * @Provides：常用于被 @Module 标记类的内部方法，并提供依赖对象
     * @Singleton：提供单例
     */
    @Provides
    @Singleton
    fun provideAppDataBase(@ApplicationContext appContext: Context): AppDataBase {
        return Room.databaseBuilder(appContext, AppDataBase::class.java, "duominuo.db")
            .fallbackToDestructiveMigration()
            .allowMainThreadQueries()
            .build()
    }


    @Provides
    @Singleton
    fun providerUserDao(appDataBase: AppDataBase): UserDao {
        return appDataBase.getUserDao()
    }


    @Provides
    @Singleton
    fun providerDownloadDao(appDataBase: AppDataBase): DownloadDao {
        return appDataBase.getDownloadDao()
    }


    @Provides
    @Singleton
    fun providerReviewTaskInfoDao(appDataBase: AppDataBase): JobWorkDao {
        return appDataBase.getReviewTaskInfoDao()
    }

    @Provides
    @Singleton
    fun providerNameMappingDao(appDataBase: AppDataBase): NameMappingDao {
        return appDataBase.getNameMappingDao()
    }
}